package com.petsbang.http;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class NetHelper {

	public NetHelper() {

	}

	/**
	 * 检测连网状态
	 * 
	 * @return
	 */
	public static String isConnectedInternet(Context context) {

		ConnectivityManager cm = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (cm == null) {
			return null;
		}

		String typeName = null;
		NetworkInfo info = cm.getActiveNetworkInfo();
		if (info != null && info.isAvailable()) {
			typeName = info.getTypeName().toLowerCase();
			if (typeName != null && !typeName.equals("wifi")) {
				typeName = info.getExtraInfo().toLowerCase();
			}
		}

		return typeName;
	}

	/**
	 * 获取Http连接对象
	 */
	public static HttpURLConnection getHttpUrlConnection(String urlString,
			String cmwap) throws Exception {
		HttpURLConnection httpConn = null;

		URL url = new URL(urlString.replaceAll(" ", "%20"));

		if (cmwap.equals("cmwap")) {
			Proxy proxy = new Proxy(java.net.Proxy.Type.HTTP,
					new InetSocketAddress("10.0.0.172", 80));
			httpConn = (HttpURLConnection) url.openConnection(proxy);
		} else {
			httpConn = (HttpURLConnection) url.openConnection();
		}

		httpConn.setConnectTimeout(20000);
		httpConn.setReadTimeout(5000);
		httpConn.setRequestProperty("Connection", "close");
		httpConn.setRequestProperty("Content-Type", "text/html;charset=utf-8");

		httpConn.setRequestMethod("GET");
		httpConn.setDoOutput(false);
		httpConn.setDoInput(true);
		httpConn.setUseCaches(false);

		return httpConn;
	}

	/**
	 * 从输入流中读取字节
	 */
	public static byte[] readBytes(InputStream in, int start, int len)
			throws Exception {
		if (start >= len) {
			return null;
		}

		ByteArrayOutputStream bo = new ByteArrayOutputStream();
		byte[] data = new byte[len];
		byte[] temp = new byte[512];
		int readLen = 0;
		int destPos = start;
		while ((readLen = in.read(temp)) > -1) {
			System.arraycopy(temp, 0, data, destPos, readLen);
			destPos += readLen;
		}

		bo.write(data, 0, destPos);
		bo.close();

		return bo.toByteArray();
	}

	/**
	 * 根据url下载图片
	 */
	public static Bitmap getImageBitmap(String url) {
		Bitmap picBitmap = null;
		try {
			URL aryURI = new URL(url);
			System.out.println("==================>" + url);

			URLConnection conn = aryURI.openConnection();
			conn.connect();

			InputStream is = conn.getInputStream();

//			picBitmap = BitmapFactory.decodeStream(is);
			
			BitmapFactory.Options o2 = new BitmapFactory.Options();
			o2.inSampleSize = 2;
			return BitmapFactory.decodeStream(is, null, o2);
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("LoadImageHelper:getVideoPictureBitmap()" + url);
		}

		return picBitmap;
	}
}
